package com.shiguo365.market;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.shiguo365.market.fragment.*;

import com.shiguo365.market.R;
import com.shiguo365.market.fragment.IndexFragment;


import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
    //UI Object
    private TextView txt_topbar;

    @BindView(R.id.txt_index)  TextView txt_index; //首页按钮
    @BindView(R.id.txt_search) TextView txt_search; //搜索按钮
    @BindView(R.id.txt_cart) TextView txt_cart; //购物车按钮
    @BindView(R.id.txt_center) TextView txt_center; //个人中心按钮
    @BindView(R.id.ly_content) FrameLayout ly_content; //用于碎片切换层

    //Fragment Object
    private IndexFragment fg1; //首页的碎片文件
    private SearchFragment fg2;
    private CenterFragment fg4;
    private MyFragment fg3;
    private FragmentManager fManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ButterKnife.bind(this); //ButterKnife初始化
        fManager = getFragmentManager();
        bindViews();
        txt_index.performClick();   //模拟一次点击，既进去后选择第一项
        this.setTitle("世果365");
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    //UI组件初始化与事件绑定
    private void bindViews() {
        txt_index.setOnClickListener(this);
        txt_search.setOnClickListener(this);
        txt_cart.setOnClickListener(this);
        txt_center.setOnClickListener(this);
    }

    //重置所有文本的选中状态
    private void setSelected(){
        txt_index.setSelected(false);
        txt_search.setSelected(false);
        txt_cart.setSelected(false);
        txt_center.setSelected(false);
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(fg1 != null)fragmentTransaction.hide(fg1);
        if(fg2 != null)fragmentTransaction.hide(fg2);
        if(fg3 != null)fragmentTransaction.hide(fg3);
        if(fg4 != null)fragmentTransaction.hide(fg4);
    }


    @Override
    public void onClick(View v) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (v.getId()){
            case R.id.txt_index:
                setSelected();
                txt_index.setSelected(true);
                if(fg1 == null){
                    fg1 = new IndexFragment();
                    fTransaction.add(R.id.ly_content,fg1);
                }else{
                    fTransaction.show(fg1);
                    this.setTitle("世果365");
                }
                break;
            case R.id.txt_search:
                setSelected();
                txt_search.setSelected(true);
                if(fg2 == null){
                    fg2 = new SearchFragment();
                    fTransaction.add(R.id.ly_content,fg2);
                }else{
                    fTransaction.show(fg2);
                    this.setTitle("全部商品");
                }
                break;
            case R.id.txt_cart:
                setSelected();
                txt_cart.setSelected(true);
                if(fg3 == null){
                    fg3 = new MyFragment("第三个Fragment");
                    fTransaction.add(R.id.ly_content,fg3);
                }else{
                    fTransaction.show(fg3);
                    this.setTitle("购物车");
                }
                break;
            case R.id.txt_center:
                setSelected();
                txt_center.setSelected(true);
                if(fg4 == null){
                    fg4 = new CenterFragment();
                    fTransaction.add(R.id.ly_content,fg4);
                }else{
                    fTransaction.show(fg4);
                    this.setTitle("个人中心");
                }
                break;
        }
        fTransaction.commit();
    }
}
